package com.example.mobifin_busticket.views.activities;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.json.JSONException;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mobifin_busticket.AppData;
import com.example.mobifin_busticket.MyApplication;
import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.UserInfo;
import com.example.mobifin_busticket.controller.callbacks.ResponseListener;
import com.example.mobifin_busticket.controller.databasemanager.DatabaseManager;
import com.example.mobifin_busticket.views.dialogs.RegistrationDialog;

public class Login extends MobifinActivity implements OnClickListener {
	SQLiteDatabase db;
	EditText et_password;
	Button btn_Go;
	TextView tv_register;
	final Context context = this;
	ProgressDialog pb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		et_password = (EditText) findViewById(R.id.et_password);
		btn_Go = (Button) findViewById(R.id.btn_go);
		tv_register = (TextView) findViewById(R.id.tv_register);

		btn_Go.setOnClickListener(this);

		tv_register.setOnClickListener(this);
		// db.execSQL("CREATE TABLE IF NOT EXISTS Registration1(Id varchar,Phone_No number,Email varchar,Terminal_No number,Password varchar,Transaction_pin number);");

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_go:
			if (validation_chk() == true) {
				DatabaseManager dbManager = new DatabaseManager(Login.this);

				UserInfo userInfo = dbManager.getUserInfo();
				dbManager.close();

				AppData.setUserInfo(userInfo);
				if (et_password.getText().toString()
						.equals(userInfo.getLoginPassword())) {
					// here we will first generate session and store in in
					// preference and then call new activity

					MyApplication.showProgressDialog(context, "Please Wait");
					final Handler handler = new Handler();
					new Thread(new Runnable() {

						@Override
						public void run() {
							MyApplication appContext = (MyApplication) Login.this
									.getApplicationContext();
							boolean sessionStatus = false;
							try {
								sessionStatus = appContext
										.getSessionId(appContext);

								if (sessionStatus == true) {
									// start menu activity

									handler.post(new Runnable() {

										@Override
										public void run() {
											// TODO Auto-generated method stub
											MyApplication
													.dismissProgressDialog();
											Toast.makeText(Login.this,
													"Login Successful",
													Toast.LENGTH_LONG).show();
											Intent intent = new Intent(
													Login.this,
													SearchBusAcitivity.class);
											startActivity(intent);
											finish();
										}
									});

								}
							} catch (NoSuchAlgorithmException e) {
								e.printStackTrace();
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							} catch (JSONException e) {
								e.printStackTrace();
							}
							// TODO Auto-generated method stub

						}
					}).start();

				} else {
					Toast.makeText(
							Login.this,
							"Login Invalid. orignal password is"
									+ userInfo.getLoginPassword(),
							Toast.LENGTH_LONG).show();
				}
			}
			break;
		case R.id.tv_register:

			RegistrationDialog registerDialog = new RegistrationDialog(
					Login.this);
			registerDialog.show();
			break;

		default:
			break;
		}

	}

	private boolean validation_chk() {
		boolean flag = true;
		if (et_password.getText().toString().trim().length() == 0) {
			flag = false;
			et_password.setError(getResources().getString(R.string.valid_pswd));

		}
		return flag;
	}

}